package org.shj.basic

object SeqDemo {
  def main(args: Array[String]): Unit = {
    val list = List(1,2,3,4)
    println(list.head)
    
    val list2 = 0::list
    println(list2)
    
    val list3 = list.++(List(5,6))
    println(list3)
    
    //Set 不可变， 不包含重复值，不保证顺序
    val set = Set(1,2,3,4,5,1)
    println(set)
    
    val set2 = set + 1
    println(set2)
    
    val set3 = set+6
    println(set3)
    
    //hashset 可变， 不包含重复值，不保证顺序
    val hashset = scala.collection.mutable.HashSet(1,2,3)
    hashset += 5
    println("hashset: " + hashset)
    
    val result =List("When I was yong", "I listen to the radio").flatMap(x => x.split(" "))
    println(result)
    println(result.map(x => (x,1)))
    println(result.map(x => (x,1)).map(x => x._2))
    
    //对于reduce，有两个参数,可以用两个占位符。如： reduce((x,y) => x+y)) 可以简化为 reduce(_+_)
    println(result.map(x => (x,1)).map(x => x._2).reduce((x,y) => x+y))
    println(result.map(x => (x,1)).map(x => x._2).reduce(_+_))
    
    //当只有一个输入参数时，可以直接使用下划线， 如： map(x => x._2) 可以用 map(_._2) 代替
    println(result.map(x => (x,1)).map(_._2).reduce(_+_)) 
    
    //使用占位符，代码的简化过程：
    List(1,2,3,4).foreach(x => print(x))
    println
    List(1,2,3,4).foreach(print(_))
    println
    List(1,2,3,4).foreach(print _ )
    println
    List(1,2,3,4).foreach(print)
  }
}